import store from "@/store";
import { createRouter, createWebHashHistory } from "vue-router";
import { initFrontEndControlRoutes } from "@/router/frontEnd";
import { initBackEndControlRoutes } from '@/router/backEnd';
import { staticRoutes } from "@/router/routes";
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { NextLoading } from '@/utils/loading';
import { resetRoute } from "@/router/utils/resetRoute";

const router = createRouter({
  history: createWebHashHistory(),
  routes: staticRoutes
});
export default router;


// isRequestRoutes 为 true，则开启后端控制路由，路径：`/src/store/modules/themeConfig.ts`
const { isRequestRoutes } = store.state.themeConfig.themeConfig;
// 前端控制路由：初始化方法，防止刷新时路由丢失
if (!isRequestRoutes) initFrontEndControlRoutes();

// 路由加载前
router.beforeEach(async (to, _, next) => {
	NProgress.configure({ showSpinner: false });
	if (to.meta.title) NProgress.start();
	// const token = Session.get('token');
  const token="2131242sdfsdfdsfs"
	if (to.path === '/login' && !token) {
		next();
		NProgress.done();
	} else {
		if (!token) {
			next(`/login?redirect=${to.path}&params=${JSON.stringify(to.query ? to.query : to.params)}`);
			// Session.clear();
			resetRoute();
			NProgress.done();
		} else if (token && to.path === '/login') {
			next('/home');
			NProgress.done();
		} else {
			if (store.state.routesList.routesList.length === 0) {
				if (isRequestRoutes) {
					// 后端控制路由：路由数据初始化，防止刷新时丢失
					await initBackEndControlRoutes();
					// 动态添加路由：防止非首页刷新时跳转回首页的问题
					// 确保 addRoute() 时动态添加的路由已经被完全加载上去
					next({ ...to, replace: true });
				}
			} else {
				next();
			}
		}
	}
});
// 路由加载后
router.afterEach(() => {
	NProgress.done();
	NextLoading.done();
});
